package com.centriole.service.workflow.entity;


import com.centriole.service.workflow.enums.FlowNode;

public class InstanceHistory extends BaseEntity {

	private static final long serialVersionUID = 7410002773053219958L;
	
	private String processId;
	private String instanceId;
	private String taskName;
	private String completeUser;
	private String completeState;
	private String remark;
	private String logicalTaskName;
	private String level;
	
	/**
	 * 获取该历史任务对应在流程图中的nodeId
	 * @return
	 */
	public String getTaskNodeId() {
		return taskName.split("-").length>=4?taskName.split("-")[0]:taskName;
	}
	
	/**
	 * 获取该历史任务对应的(作图中的)节点名字
	 * @return
	 */
	public String getTaskLogicalName() {
		logicalTaskName = taskName.split("-").length>=4?taskName.split("-")[1]:taskName;
		return logicalTaskName;
	}
	
	/**
	 * 获取该历史任务对应的节点名字
	 * @return
	 */
	public String getTaskActualName() {
		return taskName.split("-").length>=4?taskName.split("-")[2]:taskName;
	}
	
	/**
	 * 获取该历史任务对应的节点类型(userTask，examine等)
	 * @return
	 */
	public String getTaskType() {
		return taskName.split("-").length>=4?taskName.split("-")[3]:taskName;
	}
	
	public String getProcessId() {
		return processId;
	}
	public void setProcessId(String processId) {
		this.processId = processId;
	}
	public String getInstanceId() {
		return instanceId;
	}
	public void setInstanceId(String instanceId) {
		this.instanceId = instanceId;
	}
	public String getTaskName() {
		return taskName;
	}
	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}
	public String getCompleteUser() {
		return completeUser;
	}
	public void setCompleteUser(String completeUser) {
		this.completeUser = completeUser;
	}
	public String getCompleteState() {
		return completeState;
	}
	public void setCompleteState(String completeState) {
		this.completeState = completeState;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
	}

	@Override
	public String toString() {
		return "InstanceHistory [processId=" + processId + ", instanceId=" + instanceId + ", taskName=" + taskName
				+ ", completeUser=" + completeUser + ", completeState=" + completeState + ", remark=" + remark + "]";
	}
	
	/**
	 * 对于多任务节点和审批节点，加上前缀表示其类型
	 * @param taskLogicalName
	 * @return
	 */
	private String wrapTaskName(String taskLogicalName , String taskType) {
		switch (taskType) {
		case FlowNode.EXAMINE:
			return FlowNode.EXAMINE_PREFIX + taskLogicalName + ":已" + getExamineResultString();
		case FlowNode.MULTI_TASK:
			return FlowNode.MULTI_TASK_PREFIX + taskLogicalName;
		case FlowNode.MULTI_TASK_CHILD:
			return FlowNode.MULTI_TASK_PREFIX +taskLogicalName + "-" + getTaskActualName();
		default:
			return taskLogicalName;
		}
	}
	
	/**
	 * 获取添加前缀的taskLogicalName
	 * @return	比如 "财务审阅" -> "(审批)财务审阅"
	 */
	public String getWrappedTaskLogicalName() {
		return wrapTaskName( getTaskLogicalName(), getTaskType()  );
	}
	
	/**
	 * 如果有审批结果,则返回对应的结果字符串 "同意" or "驳回"
	 * @return	本身就不是审批节点调用该方法会返回null
	 */
	private String getExamineResultString() {
		String[] remarkArray = remark.split("-");
		if(remarkArray.length < 2 ) return null;
		return "1".equals( remarkArray[1] )?FlowNode.EXAMINE_AGREE:FlowNode.EXAMINE_REJECT;
	}
	
	/**
	 * 如果有审批结果,则返回对应的结果 "1"(同意) or "0"(驳回)
	 * @return	本身就不是审批节点调用该方法会返回null
	 */
	public String getExamineResult() {
		String[] remarkArray = remark.split("-");
		if(remarkArray.length < 2 ) return null;
		return remarkArray[1];
	}
	
	public String getActualRemark() {
		String[] remarkArray = (remark+"-9").split("-");
		return remarkArray.length>1?remarkArray[0]:remark;
	//	return remark;
	}
	
	/**
	 * 获取code,比如 demo_node_3-userTask, demo_node_4-multiTaskOr
	 * @return String
	 */
	public String getCode() {
		String[] taskNameArray = taskName.split("-");
		if(taskNameArray.length >= 4) {
			return taskNameArray[0] +"-" +taskNameArray[3];
		}
		return taskName;
	}
}
